package com.kfh.ybracelet.my;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.bean.Family;
import com.kfh.ybracelet.bean.UpdateFamily;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.CommonUtils;
import com.kfh.ybracelet.utils.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * 添加家庭成员页面
 * <p/>
 * Created by Administrator on 2015/11/6.
 */
public class AddFamilyDetailsActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_left;
    private TextView tv_title;
    private TextView tv_commit;
    //    private Intent mIntent;
    private EditText et_name;
    private EditText et_unit;
    private EditText et_contact_way;
    private String childID;
    private List<Family> families;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_family_count);
//        mApplication.addActivity(this);
        childID = getIntent().getStringExtra("childID");
// mIntent.putExtra("families", (Serializable)families);
        families = (List<Family>) getIntent().getSerializableExtra("families");

        init();
    }

    private void init() {
        iv_left = (ImageView) findViewById(R.id.iv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_commit = (TextView) findViewById(R.id.tv_commit);
        et_name = (EditText) findViewById(R.id.et_name);
        et_unit = (EditText) findViewById(R.id.et_unit);
        et_contact_way = (EditText) findViewById(R.id.et_contact_way);

        tv_title.setText(getResources().getString(R.string.add_family_detail));

        iv_left.setOnClickListener(this);
        tv_commit.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:

                exitAdd();

                break;

            case R.id.tv_commit://提交

                if (CommonUtils.isNull(et_name.getText().toString().trim())) {
                    showToast("请输入家庭成员人的姓名");
                    return;
                }
                if (CommonUtils.isNull(et_unit.getText().toString().trim())) {
                    showToast("请输入工作单位");
                    return;
                }
                if (CommonUtils.isNull(et_contact_way.getText().toString().trim())) {
                    showToast("请输入联系方式");
                    return;
                }

                if (!CommonUtils.isPhone(et_contact_way.getText().toString().trim())) {
                    showToast("请输入正确的手机号码");
                    return;
                }

                addHouseMembers();

                break;
        }

    }

    @Override
    public void onBackPressed() {

        exitAdd();

    }

    /**
     * 返回
     */
    private void exitAdd() {

        new AlertDialog.Builder(this).setMessage("确认退出").setPositiveButton("退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        }).setNegativeButton("取消", null).show();

    }

    /**
     * 添加家庭成员
     */
    private void addHouseMembers() {

        UpdateFamily updateFamily = new UpdateFamily();
        updateFamily.token = sp.getString(Constant.TOKEN, "");
        updateFamily.address = "";
        updateFamily.houseType = "";
        updateFamily.hukouType = "";
        updateFamily.hukouAddress = "";
        updateFamily.id = childID;

        Family family = new Family();
        family.name = et_name.getText().toString().trim();
        family.phoneNum = et_contact_way.getText().toString().trim();
        family.workUnit = et_unit.getText().toString().trim();

        updateFamily.family = new ArrayList<Family>();

        if (families != null) {
            updateFamily.family.addAll(families);
        }

        updateFamily.family.add(family);

        HttpUtil.setPostRequest("添加中...", this, Constant.WEB_ROOT + Constant.UPDATEFAMILYINFO,
                JSONObject.toJSONString(updateFamily), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

//                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {
                                showToast("添加成功");
                                finish();
//                            } else
//                                showToast(Constant.showErrorMsg(result.getInt("code"), AddFamilyDetailsActivity.this));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null);

    }

//    private Dialog dialog;

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) { //按下的如果是BACK，同时没有重复
//
//            dialog = new AlertDialog.Builder(this).setMessage("亲,是否确认退出?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialogInterface, int i) {
//                    dialog.dismiss();
//                    finish();
//                }
//            }).setNegativeButton("取消", null).show();
//
//            return true;
//        }
//
//        return super.onKeyDown(keyCode, event);
//    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }
}
